# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
#
# Copyright (C) 2007 - Łukasz Wiśniewski
# based on original Cover Art for Rhythmbox by James Livingston
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.

import rhythmdb, rb
import os	#for basename
import sys
import clutter.cluttergtk
import gtk, gobject

from CoverArtDatabase import CoverArtDatabase
from ClutterMediator import ClutterMediator

class CoverArtClutterPlugin(rb.Plugin):
	def __init__(self):
		rb.Plugin.__init__(self)
		self.widget=None
		
	def activate(self, shell):
		self.shell = shell
		
		# Connecting events to player
		self.current_entry=None
		sp = shell.get_player ()
		self.player_cb_ids = (
			sp.connect ('playing-song-changed', self.playing_entry_changed),
			sp.connect ('playing-changed', self.playing_changed)
		)

		self.art_db = CoverArtDatabase ()
		
		# Check if widget is active (hidden)
		if not self.widget==None:
			self.widget.show_all()
			return
		
		# Clutter-GTK initialization and gtk-embedding part	
		self.widget = clutter.cluttergtk.Embed()
		self.widget.set_flags(gtk.CAN_FOCUS)
		self.widget.grab_focus()
		
		# FIXME: find better way to get theme color
		# realize window otherwise it defaults to bgcolor DCDAD5
		w = gtk.Window()
		w.realize()
		style=w.get_style()
		w.destroy()
		
		# Creating ClutterMediator instance
		self.art_widget = ClutterMediator(
			self.widget.get_stage(), 
			os.path.dirname(self.find_file("coverartclutter.py"))
		)
		self.art_widget_cb_ids = (
			self.art_widget.connect('resume-playing', self.resume_playing),
			self.art_widget.connect('is-same-album', self.is_same_album),
			self.art_widget.connect('get-pixbuf', self.get_pixbuf),
			self.art_widget.connect('get-song-info', self.get_song_info)
		)
		self.art_widget.set_style(style)
		
		self.shell.add_widget (self.widget, rb.SHELL_UI_LOCATION_SIDEBAR)
		self.widget.show_all()
		
	def deactivate(self, shell):	
		self.shell = None
		self.art_db = None
	
		# Disconnect from Rhythmbox events
		sp = shell.get_player ()
		for id in self.player_cb_ids:
			sp.disconnect (id)
		
		# Disconnect from ClutterMediator events
		for id in self.art_widget_cb_ids:
			self.art_widget.disconnect (id)
		
		# FIXME: find a way to completely and safely disable clutter module on deactivation
		# shell.remove_widget (self.widget, rb.SHELL_UI_LOCATION_SIDEBAR)
		self.widget.hide()
	
	# BEGIN OF ClutterMediator callbacks
	#	
	def resume_playing (self, event):
		sp = self.shell.get_player()
		if not sp.get_playing():
			sp.play()
			
	def is_same_album (self, event, entryA, entryB):
		db = self.shell.get_property ("db")

		same_album = False
		if entryA and entryB:
			same_album = (
			   	(db.entry_get (entryA, rhythmdb.PROP_ARTIST) == db.entry_get (entryB, rhythmdb.PROP_ARTIST)) and
			   	(db.entry_get (entryA, rhythmdb.PROP_ALBUM) == db.entry_get (entryB, rhythmdb.PROP_ALBUM))
			)
		if entryA == entryB:
			same_album = True
		return same_album
		
	def get_pixbuf (self, event, entry, on_get_pixbuf_completed):
		db = self.shell.get_property ("db")
		self.art_db.get_pixbuf(db, entry, on_get_pixbuf_completed)
		
	def get_song_info (self, event, entry):
		db = self.shell.get_property ("db")
		msg = []
		if entry:
			msg.append( str( db.entry_get (entry, rhythmdb.PROP_ARTIST) ) )
			msg.append( str( db.entry_get (entry, rhythmdb.PROP_ALBUM) ) )
			msg.append( str( db.entry_get (entry, rhythmdb.PROP_TITLE) ) )
			msg.append( str( db.entry_get (entry, rhythmdb.PROP_RATING) ) )
		return msg
		#return '\n\n\n\n\n\n\n\n<span foreground="white" size="7500">'+msg+'</span>'
	#
	# END OF ClutterMediator callbacks
			
	def playing_entry_changed (self, sp, entry):	
		self.current_entry = entry
		self.art_widget.set (entry)
		
	def playing_changed (self, sp, playing):
		if playing:
			self.art_widget.pause_button.hide()
			self.art_widget.pause_button.pulse_timeline.stop()	
		elif not sp.get_playing_entry()==None:
			self.art_widget.pause_button.pulse_timeline.start()
			self.art_widget.pause_button.show()
			
		# "Palo Alto" bug in Rhythbox 0.11.2 or CoverArt [unconfirmed] #occured when listening to this song
		# When player stops playing song, it sets entry to None and playing to False
		# Resuming playing the same song sets playing to True but entry is not changed, playing_entry_changed function is not callbacked
		# Workaround unless it gets fixed
		if playing == True and self.current_entry == None:
			self.current_entry = sp.get_playing_entry()
			self.art_widget.set (self.current_entry)
			
